package com.hwy.struts.actions;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.hwy.struts.domain.Customer;
import com.hwy.struts.service.CustomerService;
import com.hwy.struts.service.impl.CustomerServiceImpl;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.ognl.OgnlValueStack;
import com.opensymphony.xwork2.ognl.OgnlValueStackFactory;
import com.opensymphony.xwork2.util.ValueStack;

public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{
	private Customer customer = new Customer();
	// 获取到所有的客户信息
	public String findAll() {
		CustomerService cService = new CustomerServiceImpl();
		List<Customer> findAll = cService.findAll();
		// 将数据放在值栈中 Root
		ValueStack valueStack = ActionContext.getContext().getValueStack();
		valueStack.set("list", findAll);
		// 值放置在Context->request
//		ServletActionContext.getRequest().setAttribute("list", findAll);
		return "findAll";
	}
	
	public String saveUI() {
		return "saveUI";
	}

	public String del() {
		HttpServletRequest request = ServletActionContext.getRequest();
		Long cust_id = Long.parseLong(request.getParameter("cust_id"));
		CustomerService cService = new CustomerServiceImpl();
		cService.del(cust_id);
		return "delSave";
	}
	// 执行保存的方法
	public String save() {
		CustomerService cService = new CustomerServiceImpl();
		cService.save(customer);
		return "successSave";
	} 
	@Override
	public Customer getModel() {
		return customer;
	}
}
